class Template < ActiveRecord::Base
  def self.search_by_tag(tag)
    self.find(:all, :conditions => "tag_line LIKE %#{tag}%")
  end
  def self.update_by_name(hash)
    t = self.find_by_name(hash[:name])
    if t
      t.update_attributes(hash)
    else
      self.create(hash)
    end
  end

  def self.search(query)
    # TODO escape
    conditions = (query.blank?) ? "" : "name LIKE \"%#{query}%\" OR tag_line LIKE \"%#{query}%\"" 
    return self.find(:all, :conditions => conditions)
  end

  def jsonize
    return { 
      :name => self.name,
      :tag_line => self.tag_line
    }
  end
end
